package com.ywj.chatroom.multiroom.server.handler;

import com.ywj.chatroom.multiroom.message.GroupQuitRequestMessage;
import com.ywj.chatroom.multiroom.message.GroupQuitResponseMessage;
import com.ywj.chatroom.multiroom.server.session.GroupSessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

//import java.nio.channels.Channel;

/**
 * 退出群消息处理器
 * 1.拿到群聊名字    2.获取群里面的人的channel
 * 3.发送消息通知
 */
@ChannelHandler.Sharable
public class GroupQuitRequestMessageHandler extends SimpleChannelInboundHandler<GroupQuitRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupQuitRequestMessage msg) throws Exception {
        String userName = msg.getUsername();
        String groupName = msg.getGroupName();
        //移除session
        GroupSessionFactory.getGroupSession().removeMember(groupName, userName);

        for(Channel channel : GroupSessionFactory.getGroupSession().getMembersChannel(groupName)){
            channel.writeAndFlush(new GroupQuitResponseMessage(true, "用户" + userName + "退出了群聊" + groupName));
        }
    }
}
